<?php
	class Controller_Offer extends Controller_Abstract
	{
	
		function actionIndex()
		{
			$offermodel = new Offer();
			
			//whether it's a search request
			if(isset($this->_context->name)){
				$name = $this->_context->name;
				$availability = $this->_context->availability;
				$status = $this->_context->status;
				$type = $this->_context->type;
				$offers = $offermodel->search($name, $availability, $status, $type);
				$this->_view['name'] = $name;
				$this->_view['availability'] = $availability;
				$this->_view['status'] = $status;
				$this->_view['type'] = $type;
			}else{
				$offers = $offermodel->getAllOffers();
			}
      	 	$this->_view['offers'] = $offers;
			
			if(isset($_SESSION['infos'])){
				$this->_view['infos'] = $_SESSION['infos'];
				unset($_SESSION['infos']);
			}
		}
		
		function actionInsert(){
			if($this->_context->isPOST()){
				$name = $this->_context->name;
				$availability = $this->_context->availability;
				$type = $this->_context->type;
				$status = $this->_context->status;
				$price = $this->_context->price;
				$convPos = $this->_context->conv_pos;
				$ipWindow = $this->_context->ip_window;
				$cookieWindow = $this->_context->cookie_window;
				
				$offermodel = new Offer();
				$offermodel->add($name, $availability, $type, $status, $price, $convPos, $ipWindow, $cookieWindow);					
				return $this->_redirect(url('offer/index'));
			}
		}
		
		function actionUpdate(){
			$id = isset($this->_context->id) ? intval($this->_context->id) : '';
			$offermodel = new Offer();
			if($this->_context->isPOST()){
				$infos = array();
				if($id){
					$name = $this->_context->name;
					$availability = $this->_context->availability;
					$ipWindow = $this->_context->ip_window;
					$cookieWindow = $this->_context->cookie_window;
					$result = $offermodel->update($name, $availability, $ipWindow, $cookieWindow, $id);		
					if($result){
						$infos[] = Util::getMessage(7007);
						$_SESSION['infos'] = $infos;
						return $this->_redirect(url('offer/index'));
					}
				}
			}elseif($this->_context->isGET()){
				if($id){
					$offer = $offermodel->getOfferForUpdating($id);
					$this->_view['id'] = $offer['id'];
					$this->_view['name'] = $offer['name'];
					$this->_view['availability'] = $offer['availability'];
					$this->_view['ip_window'] = $offer['ip_window'];
					$this->_view['cookie_window'] = $offer['cookie_window'];
				}
			}
		}
		
		function actionDelete(){
			$id = isset($this->_context->id) ? intval($this->_context->id) : '';
			if(!$id){
				$offermodel = new Offer();
				$offermodel->delete($id);
				return $this->_redirect(url('offer/index'));
			}
		}
		
		function actionClone(){
			$offermodel = new Offer();
			if($this->_context->isGET()){
				$offerId = $this->_context->id;				
				$offer = $offermodel->getOfferById($offerId);
				$this->_view['controllername'] = $this->_context->action_name;
				$this->_view['availability'] = $offer['availability'];
	        	$this->_view['name'] = $offer['name'];
	        	$this->_view['type'] = $offer['type'];
	        	$this->_view['status'] = $offer['status'];
	        	$this->_view['price'] = $offer['price'];
	        	$this->_view['conv_pos'] = $offer['conv_pos'];
	        	$this->_view['ip_window'] = $offer['ip_window'];
	        	$this->_view['cookie_window'] = $offer['cookie_window'];
	        	$this->_view['id'] = $offer['id'];
			}elseif ($this->_context->isPOST()){
				$id = $this->_context->id;
				$name = $this->_context->name;
				$availability = $this->_context->availability;
				$type = $this->_context->type;
				$status = $this->_context->status;
				$price = $this->_context->price;
				$convPos = $this->_context->conv_pos;
				$ipWindow = $this->_context->ip_window;
				$cookieWindow = $this->_context->cookie_window;
				$result = $offermodel->cloneOffer($id, $name, $availability, $type, $status, $price, $convPos, $ipWindow, $cookieWindow);
				if($result){
					$infos = array();
					$infos[] = Util::getMessage(7001);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('offer/index'));
				}
			}
		}
		
		function actionActive(){
			$infos = array();
			$offermodel = new Offer();
			$offerId = $this->_context->id;
			$result = $offermodel->setActive($offerId);
			if($result){
				$infos[] = Util::getMessage(7002);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('offer/index'));
			}
			else{
				$infos[] = Util::getMessage(7003);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('offer/index'));
			} 
		}
		
		function actionPause(){
			$infos = array();
			$offermodel = new Offer();
			$offerId = $this->_context->id;
			$result = $offermodel->setPause($offerId);
			if($result){
				$infos[] = Util::getMessage(7004);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('offer/index'));
			}
			else{
				$infos[] = Util::getMessage(7003);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('offer/index'));
			} 
		}
		
		function actionCreatives(){
			if($this->_context->isGET()){
				$cremodel = new Creative();
				$creatives = $cremodel->getListCreatives();
				$this->_view['creatives'] = $creatives;
				$rl_oc = new Ref_Oc();
				$this->_view['creative_ids'] = $rl_oc->getRelatedCreativesByOfferId($this->_context->offer_id);
				$this->_view['offer_id'] = $this->_context->offer_id;
			}elseif($this->_context->isPOST()){
				$creativesIds = $this->_context->creatives;
				$offerId = $this->_context->offer_id;
				if(null === $creativesIds){
					$offermodel = new Offer();
					if(1 == $offermodel->getStatus($offerId)){
						$infos = array();
						$infos[] = Util::getMessage(7006);
						$_SESSION['infos'] = $infos;
						return $this->_redirect(url('offer/index'));
					}
				}
				$rl_oc = new Ref_Oc();
				$selectedCreatives = $rl_oc->getRelatedCreativesByOfferId($this->_context->offer_id);
				$rl_oc = new Ref_Oc();
				$len = count($creativesIds);
				//add new creatives to offer
				for($i=0; $i<$len; $i++){
					if(null === $selectedCreatives){
						$rl_oc->add($offerId, $creativesIds[$i]);
					}
					elseif(false === array_search($creativesIds[$i], $selectedCreatives)){
						$rl_oc->add($offerId, $creativesIds[$i]);
					}	
				}
				//delete the creatives that no long related to the offer
				if(null === $creativesIds){
					if(null !== $selectedCreatives){
						foreach ($selectedCreatives as $creativeId){
							$rl_oc->deleteByOC($offerId, $creativeId);
						}
					}
				}else{
					if(null !== $selectedCreatives){
						$unrelatedCreatives = array_diff($selectedCreatives, $creativesIds);
						foreach ($unrelatedCreatives as $unrelatedCreativeId){
							$rl_oc->deleteByOC($offerId, $unrelatedCreativeId);
						}
					}
				}
				return $this->_redirect(url('offer/index'));
			}
		}
		
		function actionLandingurls(){
			if($this->_context->isGET()){
				$rl_ol = new Ref_Ol();
				$landingurls = $rl_ol->getRelatedLandings();
				$this->_view['landingurls'] = $landingurls;
				$this->_view['offer_id'] = $this->_context->offer_id;
			}elseif($this->_context->isPOST()){
				$landingurls = $this->_context->landingurls;
				
				$offerId = $this->_context->offer_id;
				
				//when unchecked all the options, check if the campaign is active
				if(null === $landingurls){
					$offermodel = new Offer();
					if(1 == $offermodel->getStatus($offerId)){
						$infos = array();
						$infos[] = Util::getMessage(7005);
						$_SESSION['infos'] = $infos;
						return $this->_redirect(url('offer/index'));
					}
				}
				
				$rl_ol = new Ref_Ol();
				$rl_ol->changeRelatedLUToOffer($offerId, $landingurls);
				
				return $this->_redirect(url('offer/index'));
			}
		}
		
		
	function actionCampaigns(){
			$campmodel = new Campaign();
			if($this->_context->isGET()){
				$campaigns = $campmodel->getAllCampaignsByOffer();
				$this->_view['campaigns'] = $campaigns;
				$this->_view['campaign_ids'] = $campmodel->getAllCampaignsRelatedToOffer($this->_context->offer_id);
				$this->_view['offer_id'] = $this->_context->offer_id;
			}elseif($this->_context->isPOST()){
				$campaignIds = $this->_context->campaigns;
				$offerId = $this->_context->offer_id;
				$selectedCampaigns = $campmodel->getAllCampaignsRelatedToOffer($this->_context->offer_id);
				$len = count($campaignIds);
				for($i=0; $i<$len; $i++){
					if(null === $selectedLandingurls){
						$campmodel->changeRelatedOffer($offerId, $campaignIds[$i]);
					}
					elseif(false === array_search($campaignIds[$i], $selectedCampaigns)){
						$campmodel->changeRelatedOffer($offerId, $campaignIds[$i]);
					}	
				}
				if(null === $campaignIds){
					if(null !== $selectedCampaigns){
						foreach ($selectedCampaigns as $campaignId){
							$campmodel->changeRelatedOffer(0, $campaignId);
						}
					}
				}else{
					if(null !== $selectedCampaigns){
						$unrelatedCampaigns = array_diff($selectedCampaigns, $campaignIds);
						foreach ($unrelatedCampaigns as $unrelatedCampaignId){
							$campmodel->changeRelatedOffer(0, $unrelatedCampaignId);
						}
					}
				}
				return $this->_redirect(url('offer/index'));
			}
		}
		
	}


